---
title: "Python3 with MySQL"
date: 2019-12-25
categories:
- python
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org2b0d016">PyMySQL :: Pure Python MySQL Client</a></li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-org2b0d016">
<h2 id="org2b0d016">PyMySQL :: Pure Python MySQL Client</h2>
<div class="outline-text-2" id="text-org2b0d016">
<pre class="example">
https://github.com/PyMySQL/PyMySQL
</pre>
<div class="org-src-container">
<pre class="src src-python"><span style="font-weight: bold;">import</span> pymysql.cursors

<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">Connect to the database</span>
<span style="font-weight: bold; font-style: italic;">connection</span> = pymysql.connect(host=<span style="font-style: italic;">'localhost'</span>,
                             user=<span style="font-style: italic;">'user'</span>,
                             password=<span style="font-style: italic;">'passwd'</span>,
                             db=<span style="font-style: italic;">'db'</span>,
                             charset=<span style="font-style: italic;">'utf8mb4'</span>,
                             cursorclass=pymysql.cursors.DictCursor)

<span style="font-weight: bold;">try</span>:
    <span style="font-weight: bold;">with</span> connection.cursor() <span style="font-weight: bold;">as</span> cursor:
        <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">Create a new record</span>
        <span style="font-weight: bold; font-style: italic;">sql</span> = <span style="font-style: italic;">"INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"</span>
        cursor.execute(sql, (<span style="font-style: italic;">'webmaster@python.org'</span>, <span style="font-style: italic;">'very-secret'</span>))

    <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">connection is not autocommit by default. So you must commit to save</span>
    <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">your changes.</span>
    connection.commit()

    <span style="font-weight: bold;">with</span> connection.cursor() <span style="font-weight: bold;">as</span> cursor:
        <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">Read a single record</span>
        <span style="font-weight: bold; font-style: italic;">sql</span> = <span style="font-style: italic;">"SELECT `id`, `password` FROM `users` WHERE `email`=%s"</span>
        cursor.execute(sql, (<span style="font-style: italic;">'webmaster@python.org'</span>,))
        <span style="font-weight: bold; font-style: italic;">result</span> = cursor.fetchone()
        <span style="font-weight: bold;">print</span>(result)
<span style="font-weight: bold;">finally</span>:
    connection.close()
</pre>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2019-12-25</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
